package com.dbl.oms.common.generate.beetl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.ResourceLoader;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;

import com.dbl.oms.common.util.ToolUtil;
import com.sun.javafx.PlatformUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class BeetlGenerator {

	/**
	 * 调整默认配置
	 * @return
	 */
	public Configuration getBeetlConfig() {
		
		Properties properties = new Properties();
		properties.put("RESOURCE.root", "");
		properties.put("DELIMITER_STATEMENT_START", "<%");
		properties.put("DELIMITER_STATEMENT_END", "%>");
		properties.put("HTML_TAG_FLAG", "##");
		Configuration cfg = null;
		try {
			cfg = new Configuration(properties);
		} catch (IOException e) {
			log.error(e.toString(),e);
		}
		return cfg;
	}

	/**
	 * 获取默认的GroupTemplate
	 * @return
	 */
	public GroupTemplate getGroupTemplate() {
		ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
		GroupTemplate groupTemplate = new GroupTemplate(resourceLoader, this.getBeetlConfig());
		groupTemplate.registerFunctionPackage("tool", new ToolUtil());
		return groupTemplate;
	}
	
	/**
	 * 获取指定resourceLoader的GroupTemplate
	 * @param resourceLoader
	 * @return
	 */
	public GroupTemplate getGroupTemplate(ResourceLoader resourceLoader) {
		GroupTemplate groupTemplate = new GroupTemplate(resourceLoader, this.getBeetlConfig());
		groupTemplate.registerFunctionPackage("tool", new ToolUtil());
		return groupTemplate;
	}
	
	/**
	 * 获取GroupTemplate
	 * @param resourceLoader
	 * @param cfg
	 * @return
	 */
	public GroupTemplate getGroupTemplate(ResourceLoader resourceLoader,Configuration cfg) {
		GroupTemplate groupTemplate = new GroupTemplate(resourceLoader, cfg);
		groupTemplate.registerFunctionPackage("tool", new ToolUtil());
		return groupTemplate;
	}
	
	public Template getTemplate(String template){
		return getGroupTemplate().getTemplate(template);
	}

	public void generateFile(Template pageTemplate, String filePath) {
		
		if (PlatformUtil.isWindows()) {
			filePath = filePath.replaceAll("/+|\\\\+", "\\\\");
		} else {
			filePath = filePath.replaceAll("/+|\\\\+", "/");
		}
		File file = new File(filePath);
		File parentFile = file.getParentFile();
		if (!parentFile.exists()) {
			parentFile.mkdirs();
		}
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream(file);
			pageTemplate.renderTo(fileOutputStream);
		} catch (FileNotFoundException e) {
			log.error("--generateFile--exception", e);
		} finally {
			try {
				fileOutputStream.close();
			} catch (IOException e) {
				log.error("--fileclose--exception", e);
			}
		}
	}

	public void generateFile(String template, String filePath) {

		Template pageTemplate = getTemplate(template);

		if (PlatformUtil.isWindows()) {
			filePath = filePath.replaceAll("/+|\\\\+", "\\\\");
		} else {
			filePath = filePath.replaceAll("/+|\\\\+", "/");
		}
		File file = new File(filePath);
		File parentFile = file.getParentFile();
		if (!parentFile.exists()) {
			parentFile.mkdirs();
		}
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream(file);
			pageTemplate.renderTo(fileOutputStream);
		} catch (FileNotFoundException e) {
			log.error("--generateFile--exception", e);
		} finally {
			try {
				fileOutputStream.close();
			} catch (IOException e) {
				log.error("--fileclose--exception", e);
			}
		}

	}
	
}
